//-------------------------------------------------------------------
//	Copyright (c) 2010-2012 Zhirnov Andrey
//	This file is part of the "UXGEN" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#ifndef _UX_CL_PROGRAM_H
#define _UX_CL_PROGRAM_H

#include "CLQueue.h"

namespace Engine
{
namespace CLSystems
{

	//
	// Program Callback
	//

	class IProgramCallback
	{
	public:
		virtual void operator () (CLProgram *pProg) = 0;
	};


	//
	// CL Program
	//

	class _EXPORT_ CLProgram : public CLBaseObject
	{
		typedef void (CL_CALLBACK *POnProgramBuild)(cl_program program, void *user_data);

	protected:
		cl_program			_pProgram;
		IProgramCallback  *	_pCallback;
		bool				_bBuilded;
		
	protected:
		virtual void _RetainRef()	const;
		virtual void _ReleaseRef()	const;
		virtual uint _GetRefCount()	const;
		virtual void _Destroy();
		void _GetOptions(cl_prog_options::type eOpt, const string_array_t &aMacroses, string &sRes) const;
		static void CL_CALLBACK _s_OnBuild(cl_program program, void *user_data);

	public:
		CLProgram();
		~CLProgram();

		bool Create(const char *pSource);
		bool Create(const char **pSources, uint uCount);
		bool Create(const string_array_t &aSources);
		bool CreateBinary(const binary_buffer_t &aData);
		virtual void  Destroy();

		bool  GetSource(string &sSource)		const;
		bool  GetBinary(binary_buffer_t &aData) const;
		bool  GetBuildOptions(string &sOptions) const;

		bool Build(cl_prog_options::type eOpt);
		bool Build(cl_prog_options::type eOpt, const string_array_t &aMacroses);

		bool  CreateKernels(array<CLKernel *> &aKernels) const;

		void  SetCallback(IProgramCallback *pCB);

		cl_program	GetIdent()	const	{ return _pProgram; }
		bool		IsBuilded()	const	{ return _bBuilded; }


		// Utils //
		static void s_Define(const string &sDef, string_array_t &sRes);
		static void s_Define(const string &sDef, const string &sVal, string_array_t &sRes);
		static void s_Include(const string &sDir, string_array_t &sRes);
	};




	inline void CLProgram::s_Define(const string &sDef, string_array_t &sRes)
	{
		sRes.PushBack( "-D " + sDef );
	}

	inline void CLProgram::s_Define(const string &sDef, const string &sVal, string_array_t &sRes)
	{
		sRes.PushBack( string("-D ") << sDef << '=' << sVal );
	}

	inline void CLProgram::s_Include(const string &sDir, string_array_t &sRes)
	{
		sRes.PushBack( "-I " + sDir );
	}

//-------------------------------------------------------------------

}	// CLSystems
}	// Engine

#endif	// _UX_CL_PROGRAM_H